//
//  ST_RecentCallsItem.swift
//  btvideo
//
//  Created by mac on 2021/9/9.
//  Copyright © 2021 JarvanZhang. All rights reserved.
//

import UIKit
import RxSwift

class ST_RecentCallsItem: UICollectionViewCell {
    
    /// 头像
    let avatar_image_view: UIImageView = {
        let imageView = UIImageView()
        imageView.backgroundColor = UIColor.random
        imageView.contentMode = .scaleAspectFill
        imageView.cornerRadius = 29
        return imageView
    }()
    
    /// 昵称
    let nickname_label: UILabel = {
        let label = UILabel()
        label.text = "Label"
        label.textColor = UIColor(hexString: "#333333")
        label.font = .systemFont(ofSize: 18, weight: .semibold)
        return label
    }()
    
    /// 在线状态
    let online_state = ST_OnLineStatusControl()
    
    /// 内容
    let content_label: UILabel = {
        let label = UILabel()
        label.text = "Label"
        label.font = .systemFont(ofSize: 14)
        return label
    }()
    
    /// 操作按钮
    let operation_button: UIButton = {
        let button = UIButton(type: .system)
        button.imageEdgeInsets = UIEdgeInsets(top: 0, left: -5, bottom: 0, right: 0)
        button.setImage(#imageLiteral(resourceName: "recentCalls_icon_voiceCall").original, for: .normal)
        button.setTitle("Button", for: .normal)
        button.setTitleColor(.white, for: .normal)
        button.titleLabel?.font = .systemFont(ofSize: 14, weight: .semibold)
        button.backgroundColor = UIColor(hexString: "#54E78E")
        button.cornerRadius = 19
        return button
    }()
    
    /// 时间
    let dateLabel: UILabel = {
        let label = UILabel()
        label.text = "Label"
        label.textColor = UIColor(hexString: "#ACB4C0")
        label.font = .systemFont(ofSize: 12)
        return label
    }()
    
    var disposeBag = DisposeBag()
    
    override func prepareForReuse() {
        super.prepareForReuse()
        
        disposeBag = DisposeBag()
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setup_views()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setup_views() {
        
        contentView.addSubview(avatar_image_view)
        avatar_image_view.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(16)
            make.centerY.equalToSuperview()
            make.height.width.equalTo(58)
        }
        
        contentView.addSubview(nickname_label)
        nickname_label.snp.makeConstraints { make in
            make.top.equalTo(avatar_image_view).offset(5)
            make.leading.equalTo(avatar_image_view.snp.trailing).offset(10)
            make.height.equalTo(25)
        }
        
        contentView.addSubview(online_state)
        online_state.snp.makeConstraints { make in
            make.centerY.equalTo(nickname_label)
            make.leading.equalTo(nickname_label.snp.trailing).offset(10)
        }
        
        contentView.addSubview(content_label)
        content_label.snp.makeConstraints { make in
            make.bottom.equalTo(avatar_image_view.snp.bottom).offset(-5)
            make.leading.equalTo(avatar_image_view.snp.trailing).offset(10)
            make.height.equalTo(20)
        }
        
        contentView.addSubview(operation_button)
        operation_button.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalToSuperview().offset(-16)
            make.width.equalTo(102)
            make.height.equalTo(38)
        }
        
        contentView.addSubview(dateLabel)
        dateLabel.snp.makeConstraints { make in
            make.top.equalTo(avatar_image_view).offset(5)
            make.trailing.equalToSuperview().offset(-16)
        }
    }
    
    func configure(for model: ST_RecentCallModel) {
        avatar_image_view.pin_setImage(from: model.targetAvatar.compress_small_image(width: 58*3).url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
        nickname_label.text = model.targetMemberName
        online_state.bind(text: model.online_state == 1 ? "在线" : "", isSelected: model.online_state == 1)
        content_label.attributedText = model.content_attributed_string
        configure_operation_button(for: model)
        dateLabel.text = model.createTime
        dateLabel.isHidden = model.sponsorSelf == false && model.type == 1
        
        
    }
    
    /// 根据数据设置button的样式
    /// - Parameter model: 最近通话
    func configure_operation_button(for model: ST_RecentCallModel) {
        
        switch model.resourceType {
        case 1:
            operation_button.setImage(#imageLiteral(resourceName: "recentCalls_icon_voiceCall").original, for: .normal)
            operation_button.setTitle("语音回拨", for: .normal)
            operation_button.backgroundColor = UIColor(hexString: "#54E78E")
        default:
            operation_button.setImage(#imageLiteral(resourceName: "recentCalls_icon_videoCall").original, for: .normal)
            operation_button.setTitle("视频回拨", for: .normal)
            operation_button.backgroundColor = UIColor(hexString: "#FEC207")
        }
     
        if model.hasCallback == 1 {
            operation_button.isHidden = true
        } else {
            if model.sponsorSelf {
                operation_button.isHidden = true
            } else {
                operation_button.isHidden = model.type != 1
            }
        }
        
    }
}

extension ST_RecentCallModel {
    
    var content_attributed_string: NSAttributedString {
        let att = NSMutableAttributedString()
        
        if type == 1 {
            att.append(NSAttributedString(string: "\(resourceType == 1 ? "语音" : "视频")未接听 ", attributes: [.foregroundColor: #colorLiteral(red: 0.9215686275, green: 0.2352941176, blue: 0.2352941176, alpha: 1), .font: UIFont.systemFont(ofSize: 14)]))
            att.append(NSAttributedString(string: duration, attributes: [.foregroundColor: #colorLiteral(red: 0.6745098039, green: 0.7058823529, blue: 0.7529411765, alpha: 1), .font: UIFont.systemFont(ofSize: 12)]))
        } else {
            att.append(NSAttributedString(string: "语音通话：\(duration)", attributes: [.foregroundColor: #colorLiteral(red: 0.6745098039, green: 0.7058823529, blue: 0.7529411765, alpha: 1), .font: UIFont.systemFont(ofSize: 14)]))
        }
        
        return att
    }
}
